package com.tojabo.thread;

import java.util.LinkedList;
import java.util.List;

public class ThreadPool {

	// 线程池中默认线程的个数为5 
	private static int worker_num = 5;
	// 工作线程
	private WorkThread[] wordThreads;
	
	// 未处理的任务  
    private static volatile int finished_task = 0;
    // 任务队列，作为一个缓冲,List线程不安全  
    private List<Runnable> taskQueue = new LinkedList<Runnable>();
    
    private static ThreadPool threadPool;
    
    // 创建具有默认线程个数的线程池  
    private ThreadPool() {  
        this(5);  
    }
    
    // 创建线程池,worker_num为线程池中工作线程的个数  
    private ThreadPool(int worker_num) {  
        ThreadPool.worker_num = worker_num;  
        wordThreads = new WorkThread[worker_num];  
        for (int i = 0; i < worker_num; i++) {
        	wordThreads[i] = new WorkThread();
        	wordThreads[i].start();// 开启线程池中的线程  
        }  
    } 
    
    // 单态模式，获得一个默认线程个数的线程池  
    public static ThreadPool getThreadPool() {  
        return getThreadPool(ThreadPool.worker_num);  
    }
    
    // 单态模式，获得一个指定线程个数的线程池,worker_num(>0)为线程池中工作线程的个数  
    // worker_num<=0创建默认的工作线程个数  
    public static ThreadPool getThreadPool(int worker_num1) {
    	
    	if(worker_num1<=0){
    		worker_num1=ThreadPool.worker_num;
    	}
    	if (threadPool == null)  
            threadPool = new ThreadPool(worker_num1);
    	
    	return threadPool;
    }
    
    // 批量执行任务,其实只是把任务加入任务队列，什么时候执行有线程池管理器决定
    public void execute(Runnable[] task) {  
        synchronized (taskQueue) {  
            for (Runnable t : task)  
                taskQueue.add(t);  
            taskQueue.notify();  
        }  
    }
    
    // 批量执行任务,其实只是把任务加入任务队列，什么时候执行有线程池管理器决定
    public void execute(List<Runnable> task) {  
        synchronized (taskQueue) {  
            for (Runnable t : task)  
                taskQueue.add(t);  
            taskQueue.notify();  
        }  
    }
    
    // 销毁线程池,该方法保证在所有任务都完成的情况下才销毁所有线程，否则等待任务完成才销毁  
    public void destroy() {
    	while (!taskQueue.isEmpty()) {// 如果还有任务没执行完成，就先睡会吧  
            try {  
                Thread.sleep(10);  
            } catch (InterruptedException e) {  
                e.printStackTrace();  
            }  
        }
    	
    	// 工作线程停止工作，且置为null  
        for (int i = 0; i < worker_num; i++) {  
        	wordThreads[i].stopWorker();  
        	wordThreads[i] = null;  
        }  
        threadPool=null;  
        taskQueue.clear();// 清空任务队列
    }
    
    /** 
     * 内部类，工作线程 
     */  
    private class WorkThread extends Thread { 
    	// 该工作线程是否有效，用于结束该工作线程  
        private boolean isRunning = true;
        
        /* 
         * 关键所在啊，如果任务队列不空，则取出任务执行，若任务队列空，则等待 
         */
        public void run() {
        	Runnable r = null;
        	while (isRunning) { //while 1
        		
        		synchronized (taskQueue) {
        			while (isRunning && taskQueue.isEmpty()) {// while 2
        				try {  
                            taskQueue.wait(20);
                        } catch (InterruptedException e) {  
                            e.printStackTrace();  
                        }
        			}// end while 2
        			if(!taskQueue.isEmpty()){
        				r = taskQueue.remove(0);// 取出任务
        			}       			
        		}
        		
        		if (r != null) {  
                    r.run();// 执行任务  
                }  
                finished_task++;  
                r = null;
        		
        	}// end while 1
        	      	
        } //end run()
        
        // 停止工作，让该线程自然执行完run方法，自然结束
        public void stopWorker(){
        	isRunning = false;
        }
        
    } //end thread 
}
